const fs = require("fs")
const template=require("art-template")
function httpRequest(path, response) {
    fs.readFile(path, function (error,data) {
        if (error) {
            response.setHeader("Content-Type", "text/plain;charset=UTF-8");
            response.write("资源加载错误");
            response.end();
        } else {
            response.end(data);
        }
    })
}



function httpFileRequest(path, response,data) {
    fs.readFile(path, function (error, fileData) {
        if (error) {
            response.setHeader("Content-Type", "text/plain;charset=UTF-8");
            response.write("资源加载错误");
            response.end();
        } else {
            response.end(template.render(fileData.toString(),data));
        }
    })
}
function readFile(path) {
    return new Promise((reslove, reject) => {
        fs.readFile(path, function (error,data) {
            if (error) {
                reject(error)
            } else {
                reslove(data)
            }
        })
    })
}
function writeFile(path,content) {
    return new Promise((reslove, reject) => {
        fs.writeFile(path, content, function (error) {
            if (error) {
                reject(error)
            } else {
                reslove("更新成功")
            }
        })
    })
}
module.exports = {
    httpRequest,
    httpFileRequest,
    readFile,
    writeFile
}